package model

import (
	"demo/config"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

var DB *gorm.DB

func init()  {

	c := config.Conf.GetStringMapString("mysql")

	dsn := c["username"] + ":" + c["password"] + "@tcp(" + c["host"] + ":" + c["port"] + ")/" + c["database"] + "?charset=utf8&parseTime=True"

	db, err := gorm.Open("mysql", dsn)
	if err != nil {
		panic("database connection error!")
	}

	db.SingularTable(true)
	gorm.DefaultTableNameHandler = func (db *gorm.DB, defaultTableName string) string  {
		return "f_" + defaultTableName
	}

	DB = db
}